<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>云校通-教师-学生成绩登记</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
<script src="<%=basePath %>/js/select.js"></script>
<body ontouchstart class="js-jf">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui-header bg-blue"> 
		  		<div class="weui-header-left"> <a onclick="back()" class="icon icon-109 f-white">返回</a > </div>
		    	<h1 class="weui-header-title">学生成绩登记</h1>
		  	</div>
			<div class="weui_cells weui_cells_access mt0">
				<div class="weui_cell xtbg-add-list-title">
					<div class="weui_cell_bd weui_cell_primary tm f-green">
						${term.termName }
					</div>
				</div>
				<div class="weui_cell">
			        <div class="weui_cell_hd">
			          	<label class="weui_label">考试名称</label>
			        </div>
         		  	<div class="weui_cell_bd weui_cell_primary">
						<input id="examKey" class="weui_input weui_input-select tr" placeholder="请选择" readonly/>
	      			</div>
	      			<div class="weui_cell_ft"></div>
	    		</div>
	    		<div class="weui_cell">
		            <div class="weui_cell_hd">
		             	<label class="weui_label label3">考试科目</label>
		            </div>
		            <div class="weui_cell_bd weui_cell_primary" id="typeInput">
		  				<input id="examSubject" class="weui_input weui_input-select tr main1" placeholder="请选择" readonly/>
		            </div>
		            <div class="weui_cell_ft"></div>
  	      		</div>
				<div class="weui_cell">
			        <div class="weui_cell_hd">
			            <label class="weui_label label3">班级</label>
			        </div>
			        <div class="weui_cell_bd weui_cell_primary">
						<input type="text" id="classId" class="weui_input weui_input-select tr main1" placeholder="请选择" readonly/>
			        </div>
			        <div class="weui_cell_ft"></div>
		        </div>
		        <div class="weui_cell box-shadow f15">
				    <div class="weui_cell_bd weui_cell_primary">
				        <p>登分老师</p>
				    </div>
				    <div class="weui_cell_bd">
				      <p>${user.teacher.name}</p>
				    </div>
				</div>
  	      	</div>
  	      	<div class="weui_cells weui_cells_form mt0 hide" id="studentPanel">
				<div class="weui_cell" style="background: #f8f8f8;">
          			<div class="weui_cell_hd">
            			<label class="weui_label label3">班级名单</label>
          			</div>
	      		</div>
	      		<ul class="lb_show1">
	      			<li>
	      				<p class="f-red w33">学号</p>
	      				<p class="f-red w33">姓名</p>
	      				<p class="f-red w33">成绩</p>
	      			</li>
	      		</ul>
		      	<ul class="lb_show1" id="studentList">
		      	</ul>
		     </div>
  	      	<div class="weui_btn_area">
	      		<a href="javascript:;" id="regSubmit" class="weui_btn bg-blue">确认提交</a>
	    	</div>  
		</div>
	</div>
	
	<div class="hide_bd1"></div>
	<div class="hb_choose type2" style="top: 200px;width: 100%;left:0;">
		<div class="bj">
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
$(function() {
	$("#classId").click(function() {
		if($("#examSubject").attr("data-values") == null) {
			$.alert("请先选择科目", "");
			return;
		}
    	$(".type2").show();
		$(".hide_bd1").attr("style", "display: block;");
	})
	$(document).on("click", ".hb_choose .title>li", function(e) {
		$(this).addClass("active").siblings().removeClass("active");
		$(".yng").hide();
		$("#" + $(this).attr("data-key")).show();
   	});
	$(document).on("click", ".name_show_box li", function(e) {
    	var teacherId = $(this).attr("data-id");
    	var teacherName = $(this).attr("data-name");
    	$('.name_show_box ul').html('');
    	$('.name_show_box').hide();
    	$("#weui_search_input").val(null);
   		$('.name_show').find('ul').append('<li onclick="listDel(this)" class="list-del" data-id="'+teacherId+'" data-name="'+teacherName+'">'+teacherName+'<span class="list-del-vaild"></span></li>');
    });
	$(document).on("change", ".list_3 input", function(e) {
		var list = $(".list_3 input:checked");
		list.each(function(i, v){
			v.checked = false;
		});
		this.checked = true;
		var grade = $(this).attr("data-grade");
		var clazz = $(this).attr("data-clazz");
		loadRegStudent(grade, clazz);
		$("#classId").val($(this).attr("data-name"));
		$(".type2").hide();
		$(".hide_bd1").attr("style", "display: none;");
	});
	$(document).on("blur", "#studentList input", function(e) {
		var score = emptyToNull($(this).val());
		if(score != null) {
			var subjectScore = parseInt($("#examSubject").attr("data-values"));
			if(parseInt(score) > subjectScore) {
				$.alert("本次考试" + $("#examSubject").val() + "满分值是" + subjectScore, "");
				$(this).val('');
			}
		}
	})
	$("#regSubmit").click(function() {
		regSubmit();
	})
    loadRegExam();
})
function listDel(thiz){
  	$(thiz).remove();
}
var examMap = new Map();
function loadRegExam() {
	$.ajax({
		url: basePath + "/ls/exam/score/regExam",
		type: 'POST',
		dataType : "json",
		success: function(data) {
			if(data != null && data.length) {
				var items = [];
				for(var i in data) {
					items.push({title: data[i].examName, value: data[i].examKey});
					examMap.set(data[i].examKey, data[i]);
				}
				$("#examKey").select({
			        items: items,
			        onChange: function(d) {
			        	loadSubject(d.values);
			        }
			   	});
			}else {
				$.alert("暂无可等分的考试","");
			}
		},
		error: function(data) {
			alert("系统异常,请联系管理员");
		}
	});
}
function loadSubject(examKey) {
	$("#examSubject").remove();
	$("#typeInput").html('<input type="text" id="examSubject" class="weui_input weui_input-select tr main1" value="请选择" readonly="readonly"/>');
	var scores = examMap.get(examKey).cmeFullScore.split(",");
	$("#examSubject").select({
        items: [
          {title: "语文", value: scores[0]},
          {title: "数学", value: scores[1]},
          {title: "英语", value: scores[2]}
        ],
        onChange: function(d) {
        	loadClass(examKey, d.titles);
        }
   	});
}
function loadClass(examKey, subjectName) {
	$.ajax({
		url: basePath + "/ls/exam/score/examReg/class",
		type: 'POST',
		data: {
			examKey: examKey,
			subjectName: subjectName
		},
		dataType : "json",
		success: function(data) {
			if(data != null) {
				$("#classId").attr("disabled", null);
				var html = '<ul class="title">';
				for(var i in data) {
					var itm = data[i];
					html += '<li class="'+(i == 0 ? "active " : "")+'" data-key="s'+itm.grade+'">'+itm.gradeText+'</li>';
				}
				html += "</ul>";
				for(var i in data) {
					var itm = data[i];
					html += '<div class="yng" id="s'+itm.grade+'"><ul class="list_3">';
					for(var j in itm.classList) {
						var itm1 = itm.classList[j];
						var gradeClass = itm1.grade + "" + (itm1.clazz < 10 ? '0' + itm1.clazz : itm1.clazz);
						if(itm1.scoreTeacherName != null && itm1.scoreTeacherName.length > 0) {
							html += '<li><div class="weui_cell_hd list_c3">\
								<label class="weui_cells_checkbox weui_check_label" for="'+gradeClass+'">\
								<input type="checkbox" class="weui_check" disabled id="'+gradeClass+'">\
	    						</label>'+itm1.clazz+'班\
	    						<span class="weui_badge_cust">已录</span>\
							</div></li>';
						}else {
							html += '<li><div class="weui_cell_hd list_c3">\
								<label class="weui_cells_checkbox weui_check_label" for="'+gradeClass+'">\
								<input type="checkbox" class="weui_check" data-grade="'+itm1.grade+'" data-clazz="'+itm1.clazz+'" data-name="'+itm.gradeText+"(" + itm1.clazz +"班)"+'" id="'+gradeClass+'">\
	    						<i class="weui_icon_checked"></i>\
	    						</label>'+itm1.clazz+'班\
							</div></li>';
						}
					}
					html += '</ul></div>';
				}
				$(".bj").html(html);
				$("#s" + data[0].grade).show();
			}
		},
		error: function(data) {
			alert("系统异常,请联系管理员");
		}
	});
}
function loadRegStudent(grade, clazz) {
	$.ajax({
		url: basePath + "/ls/exam/score/student/query",
		type: 'POST',
		data: {
			grade: grade,
			clazz: clazz
		},
		dataType : "json",
		success: function(data) {
			if(data != null) {
				$("#studentPanel").show();
				var html = '';
				for(var i in data) {
					var itm = data[i];
					html += '<li><p class="w33">'+itm.studentId+'</p>\
		      			<p class="w33">'+itm.name+'</p>\
		      			<p class="w33"><input class="weui_input tm score_input weui_input_border f-red" data-id="'+itm.id+'"  type="number" pattern="[0-9]*" value="98.5"></p>\
		      		</li>';
				}
				$("#studentList").html(html);
			}
		},
		error: function(data) {
			alert("系统异常,请联系管理员");
		}
	});
}
function regSubmit(){
	var examKey = $("#examKey").val().trim();
	if(examId.length == 0){
		$.alert("请选择考试名称","");
		return;
	}
	var examSubject = $("#examSubject").val().trim();
	if(examSubject.length == 0) {
		$.alert("请选择考试科目","");
		return;
	}
	var examSubject = $("#examSubject").val().trim();
	if(examSubject.length == 0) {
		$.alert("请选择考试科目","");
		return;
	}
	var classId = $("#classId").val().trim();
	if(classId.length == 0) {
		$.alert("请选择班级","");
		return;
	}
	var count = 0;
	var len = $("#studentList input").length;
	var scoreList = [];
	for(var i = 0; i < len; i++) {
		var obj = $($("#studentList input")[i]);
		var v = obj.val().trim();
		if(v=='') {
			count += 1;
		}else {
			scoreList.push({"studentId": parseFloat(obj.attr("data-id")), "score": parseFloat(v)});
		}
	}
	console.log(scoreList);
	if(count > 0) {
		$.alert("还有 " + count + "位学生分数未填，请核实后再提交");
		return;
	}
	var data = {};
	data.examId = examMap.get(examKey).id;
	
	$.ajax({
		url: basePath + "/ls/exam/score/regScoreSubmit",
		type: 'POST',
		data: JSON.stringify(data),
		dataType : "json",
		success: function(data) {
			if(data != null) {
				$("#studentPanel").show();
				var html = '';
				for(var i in data) {
					var itm = data[i];
					html += '<li><p class="w33">'+itm.studentId+'</p>\
		      			<p class="w33">'+itm.name+'</p>\
		      			<p class="w33"><input class="weui_input tm score_input weui_input_border f-red" data-id="'+itm.id+'"  type="number" pattern="[0-9]*" value=""></p>\
		      		</li>';
				}
				$("#studentList").html(html);
			}
		},
		error: function(data) {
			alert("系统异常,请联系管理员");
		}
	});
}
</script>
</html>